/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import exceptions.GenericException;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateTemplate;

/**
 *
 * @author tomasz
 */
public class FileDAO implements File{

    private SessionFactory sessionFactory;
    private HibernateTemplate hibernateTemplate;
    
    
     public void setSessionFactory(SessionFactory  sessionFactory) {
            this.sessionFactory = sessionFactory;
            this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }
     
    public void addFile(model.File file) {
        
         try{
            
            this.hibernateTemplate.saveOrUpdate(file);
            
        }catch(DataAccessException accessException){
            
           throw new GenericException(accessException.getMessage() );
           
        }
        
        
    }
    
    
    
    public void addAccount(model.Account account) {
        try{
            
            this.hibernateTemplate.saveOrUpdate(account);
            
        }catch(DataAccessException accessException){
            
           throw new GenericException(accessException.getMessage() );
           
        }
    }

    
    public List allFileAccount(int idAccount) {
        
        Session session = this.sessionFactory.openSession();
        List<model.File> fi =  session.createQuery("from File WHERE idAccount="+idAccount).list();

        return fi;
        
    }

    public model.File findFileId(int id) {
        
        Session session = this.sessionFactory.openSession();
        model.File file = (model.File) session.createQuery("from model.File WHERE idFile="+id).uniqueResult();
        
        return file;
    }
    
    
}
